<template>
  <div>
    <Nav message="-1" />
    <van-address-edit
      :area-list="areaList"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
    />
  </div>
</template>
<script>
import AreaList from "../api/area";
import { Toast } from "vant";
import Nav from "@/components/Nav.vue";
export default {
  components: {
    Nav,
  },
  created() {
    let skuId = this.$route.query.skuId;
    let skuNum = this.$route.query.skuNum;
    this.detailGoAddressSkuId = skuId;
    this.detailGoAddressSkuNum = skuNum;
  },
  data() {
    return {
      areaList: AreaList,
      searchResult: [],
      newAddress: {
        areaCode: "330106",
        name: "Anni",
        tel: "13000000000",
        address: "浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室",
        token: null,
      },
      detailGoAddressSkuId: null, //详情页跳地址
      detailGoAddressSkuNum: null, //详情页跳地址
    };
  },
  methods: {
    onSave(item) {
      console.log("vant的地址：");
      console.log(item);

      let token = this.$cookie.get("shoptoken");
      if (token == null) {
        this.$router.push({
          path: "/login",
          query: { item: this.$route.path },
        });
      }

      this.newAddress.areaCode = item.areaCode;
      this.newAddress.name = item.name;
      this.newAddress.tel = item.tel;
      this.newAddress.address =
        item.province + item.city + item.county + "&" + item.addressDetail;
      this.newAddress.token = token;

      console.log("新增的地址：");
      console.log(this.newAddress);

      const _this = this;
      var newAddressJSON = JSON.stringify(_this.newAddress);

      console.log("新增的地址JSON：");
      console.log(newAddressJSON);

      axios
        .post(this.$api + "member/newBuyerAddress", newAddressJSON, {
          headers: {
            "Content-Type": "application/json;charset=UTF-8",
          },
        })
        .then(function (resp) {});

      let instance = Toast("添加成功");
      setTimeout(() => {
        instance.close();
        this.$router.push({
          path: "/addressList",
          query: {
            skuId: this.detailGoAddressSkuId,
            skuNum: this.detailGoAddressSkuNum,
          },
        });
      }, 1000);
    },
  },
};
</script>
<style scoped>
::v-deep .van-button {
  background: rosybrown;
  border: rosybrown;
  font-weight: bold;
}
</style>